package com.link.base.base.cachemanager.service;

import javax.annotation.Resource;

import com.link.base.base.cachemanager.dao.mybatis.mapper.CacheManagerMapper;
import com.link.base.base.cachemanager.model.CacheManager;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.RedisUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.List;
import java.util.Set;

/**
 * 缓存器管理-缓存器管理 
 *
 * @author 秦学刚
 * @date 2020/05/08 09:23
 */
@Service
public class CacheManagerServiceImpl extends BasicServiceImpl<CacheManager> implements CacheManagerService {
    private static Logger logger = LoggerFactory.getLogger(CacheManagerServiceImpl.class);

    private static final String CACHE_KEY = "CACHE_KEY";

    /** 缓存前缀 */
    private static final String PREFIX = "lnk_dc_";

    @Resource
    private CacheManagerMapper cacheManagerMapper;
    
    @Override
    public BasicMapper<CacheManager> getBasicMapper() {
        return cacheManagerMapper;
    }
    
    
    /**
     * 新建前处理
     * 
     * @author 秦学刚
     * @date 2020/5/8 
     * @param entity
     * @return
     **/
    @Override
    public void beforInsert(CacheManager entity) throws Exception {
        QueryParams qps = new QueryParams();
        qps.setSort("cacheKey");
        qps.setOrder("desc");
        CacheManager temp = CacheManager.transformClass(new CacheManager(), qps);
        List<CacheManager> cacheManagers = cacheManagerMapper.queryByExamplePage(temp);
        if (cacheManagers == null || cacheManagers.size() <= 0) {
            entity.setCacheKey("CACHE_00001");
        } else {
            String cacheKey = cacheManagers.get(0).getCacheKey();
            Integer integer = Integer.valueOf(cacheKey.substring(6, cacheKey.length()));
            NumberFormat formatter = NumberFormat.getNumberInstance();
            formatter.setMinimumIntegerDigits(5);
            formatter.setGroupingUsed(false);
            entity.setCacheKey("CACHE_" + formatter.format(integer + 1));
        }
    }


    @Override
    public void refreshCache() throws Exception {
        LinkRedisClient linkRedisClient = null;
        try {
            linkRedisClient = RedisUtil.getJedis();
            QueryParams qps = new QueryParams();
            qps.setSort("cacheKey");
            qps.setOrder("desc");
            qps.setPageFlag(false);
            CacheManager temp = CacheManager.transformClass(new CacheManager(), qps);
            List<CacheManager> cacheManagers = cacheManagerMapper.queryByExamplePage(temp);
            linkRedisClient.del(CACHE_KEY);
            for (CacheManager cacheManager : cacheManagers) {
                linkRedisClient.hset(CACHE_KEY, cacheManager.getCacheKey(), cacheManager.getCacheTime());
            }
        } catch (Exception e) {
            logger.info("刷新缓存器缓存失败：" + e.getMessage());
        } finally {
            RedisUtil.returnResource(linkRedisClient);
        }
    }

    @Override
    public void clearCache(CacheManager cacheManager) throws Exception {
        cacheManager = this.queryById(cacheManager);
        LinkRedisClient linkRedisClient = null;
        try {
            linkRedisClient = RedisUtil.getJedis();
            String key = cacheManager.getCacheKey();
            Set<String> keys = linkRedisClient.keys(PREFIX + key + "*");
            if (keys != null) {
                for (String str : keys) {
                    linkRedisClient.del(str);
                }
            }
        } catch (Exception e) {
            throw e;
        } finally {
            RedisUtil.returnResource(linkRedisClient);
        }
    }
}

